package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import com.sky.vo.DishItemVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    public SetmealDTO getById  (Long id){
        //根据套餐id查询套餐数据
        Setmeal setmeal=setmealMapper.getById(id);
        //根据套餐id查询套餐菜品数据
        List<SetmealDish> list=setmealDishMapper.getBySetmealId(id);
        //
        SetmealDTO setmealDTO =new SetmealDTO();
        BeanUtils.copyProperties(setmeal,setmealDTO);
        setmealDTO.setSetmealDishes(list);
        return setmealDTO;

    }

    /**
     * 新增套餐
     * @param setmealDTO
     */
    public void saveWithDishes(SetmealDTO setmealDTO) {
        //插入setmeal数据表一条数据;
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.insert(setmeal);
        //获取insert语句生成的setmealId：
        Long setmealId = setmeal.getId();
        //插入setmealdish数据表n条数据;
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes!=null&&setmealDishes.size()>0){
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmealId);
            });
            setmealDishMapper.insertBatch(setmealDishes);
        }
    }

    /**
     * 分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 批量删除
     * @param ids
     */
    public void delete(List<Long> ids) {
        //判断该套餐是否为起售
        for (Long id : ids) {
            Setmeal s = setmealMapper.getById(id);
            if (s.getStatus() == StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        //删除setmeal中的数据;
        setmealMapper.delete(ids);
        //删除setmeal附带的setmealdish数据;
        setmealDishMapper.delete(ids);
    }

    /**
     * 修改套餐
     * @param setmealDTO
     */
    public void update(SetmealDTO setmealDTO) {
        //更新setmeal表中的一条数据
        Setmeal setmeal =new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.update(setmeal);
        //删除本来的数据
        setmealDishMapper.deleteById(setmeal.getId());
        //更新setmealDish中的n条数据
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes!=null&&setmealDishes.size()>0){
            setmealDishes.forEach(setmealDish -> {
                //再插入
                setmealDish.setSetmealId(setmealDTO.getId());
            });
            setmealDishMapper.insertBatch(setmealDishes);
        }
    }

    /**
     * 套餐起售、停售
     * @param status
     * @param id
     */
    public void startOrStop(Integer status, Long id) {
        //判断套餐里面是否有停售的商品
        if (status==StatusConstant.ENABLE){
            //获得setmealDishes列表
            List<SetmealDish> setmealDishes = setmealDishMapper.getBySetmealId(id);
            if(status == StatusConstant.ENABLE){
                //select a.* from dish a left join setmeal_dish b on a.id = b.dish_id where b.setmeal_id = ?
                List<Dish> dishList = dishMapper.getBySetmealId(id);
                if(dishList != null && dishList.size() > 0){
                    dishList.forEach(dish -> {
                        if(StatusConstant.DISABLE == dish.getStatus()){
                            throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                        }
                    });
                }
            }
        }
        //将状态赋值
        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        //更新该套餐
        setmealMapper.update(setmeal);
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
